/**
 * <copyright>
 *
 * Copyright (c) 2011 Zeligsoft (2009) Limited
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   Zeligsoft (2009) Limited - Initial API and implementation
 *
 * </copyright>
 *
 */
package org.omg.deployment.provider.util;

import java.util.List;

import org.omg.deployment.Any;
import org.omg.deployment.DataType;
import org.omg.deployment.DataValue;
import org.omg.deployment.TCKind;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

/**
 * @author Toby McClean (tmcclean)
 *
 */
public enum AnyUtil {
	INSTANCE;
	
	public String toString(Any theAny) {
		List<String> values = 
			Lists.transform(theAny.getValue(), new AnyValues2StringList(theAny.getType()));
		
		return Joiner.on(", ").skipNulls().join(values);
	}
	
	private class AnyValues2StringList implements Function<DataValue, String> {

		private DataType type = null;
		
		
		public AnyValues2StringList(DataType dataType) {
			type = Preconditions.checkNotNull(dataType);;
		}
		
		@Override
		public String apply(DataValue input) {
			String result = "";
			if(type.getKind() == TCKind.TK_STRING) {
				result = Joiner.on(", ").skipNulls().join(input.getString());
			}
			
			return result;
		}
		
	}
}
